package com.example.administrator.dc.fragment;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.dc.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyHomeFragment extends Fragment implements  View.OnClickListener {
    private LinearLayout mTabCampus;
    private LinearLayout mTabUserphoto;
    private LinearLayout mTabAll;

    private TextView tvCampus;
    private TextView tvUserphoto;

    private android.support.v4.app.Fragment mFmCampus;
    private android.support.v4.app.Fragment mFmUserphoto;
    private  View view;

/**/
    public MyHomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_myhome, container, false);
        mTabCampus=(LinearLayout)view.findViewById(R.id.id_tab_campus);
        mTabUserphoto=(LinearLayout)view.findViewById(R.id.id_tab_userphoto);
        mTabAll=(LinearLayout)view.findViewById(R.id.id_tab_myhome_top_all);

        tvCampus=(TextView)view.findViewById(R.id.campus_tv);
        tvUserphoto=(TextView)view.findViewById(R.id.userphoto_tv);
        //initViews();
        initEvents();
        setSelect(0);
       /* */
        return view;
    }
    private void initEvents(){
        mTabCampus.setOnClickListener(this);
        mTabUserphoto.setOnClickListener(this);
    }
    private  void initViews(){

    }
    private void resetTextColors() {

        tvCampus.setTextColor(ContextCompat.getColor(getActivity(), R.color.myhome_top_text_color));
        tvUserphoto.setTextColor(ContextCompat.getColor(getActivity(), R.color.myhome_top_text_color));
    }
    private  void resetBgColors(){
        mTabCampus.setBackgroundResource(R.drawable.shape_myhome_top);
        mTabUserphoto.setBackgroundResource(R.drawable.shape_myhome_top);
        //mTabUserphoto.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));


    }

    @Override
    public void onClick(View v) {

        resetTextColors();
        resetBgColors();
        switch (v.getId())
        {
            case R.id.id_tab_campus:
                setSelect(0);
                break;
            case R.id.id_tab_userphoto:
                setSelect(1);
                break;

            default:
                break;
        }
    }
    private void setSelect(int i) {
        FragmentManager fm =getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mFmCampus == null)
                {
                    mFmCampus = new CampusFragment();
                    transaction.add(R.id.id_content_myhome, mFmCampus);
                } else
                {
                    transaction.show(mFmCampus);
                }
                tvCampus.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                mTabCampus.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.myhome_top_text_color));

                break;
            case 1:
                if (mFmUserphoto == null)
                {
                    mFmUserphoto = new UserphotoFragment();
                    transaction.add(R.id.id_content_myhome, mFmUserphoto);
                } else
                {
                    transaction.show(mFmUserphoto);

                }

                tvUserphoto.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                mTabUserphoto.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.myhome_top_text_color));

                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mFmCampus != null)
        {
            transaction.hide(mFmCampus);
        }
        if (mFmUserphoto != null)
        {
            transaction.hide(mFmUserphoto);
        }

    }
   /* */

}
